package com.slaffka.controllers;

import javax.servlet.AsyncContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Timer;
import java.util.TimerTask;


/**
 * Created by IntelliJ IDEA.
 * _User: v.tsapaev
 * Date: 20.02.12
 * Time: 20:34
 * To change this template use File | Settings | File Templates.
 */
@WebServlet(name = "newServlet", asyncSupported = true)
public class NewServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        final AsyncContext aCtx = request.startAsync(request, response);
        Timer timer = new Timer();
        TimerTask task = new TimerTask() {
            @Override
            public void run() {
                System.out.println("timer run");
                new AsyncWebService(aCtx);
            }
        };
        timer.schedule(task, 100);

        System.out.println("aaaa");
    }

    private class AsyncWebService implements Runnable {
        AsyncContext ctx;
        public AsyncWebService(AsyncContext ctx) {
            this.ctx = ctx;
        }
        public void run() {
            // Invoke web service and save result in request attribute
            // Dispatch the request to render the result to a JSP.
            ctx.dispatch("newServlet");
        }
    }
}
